var http = require('http');
var AipOcrClient = require("baidu-aip-sdk").ocr;
var fs = require('fs');

// 设置APPID/AK/SK
APP_ID = '22668230'	

API_KEY = 'y1pQFieGhcUKElv3vYmHMOZn'
SECRET_KEY = 'ymHjKYl9RSKZW08Fg18I6ckB1xldlGKM'
const title = "下行-2车道 "

String.prototype.splice = function(start, newStr) {
    return this.slice(0, start) + newStr + this.slice(start);
};

// 新建一个对象，建议只保存一个对象调用服务接口
var client = new AipOcrClient(APP_ID, API_KEY, SECRET_KEY);
function rename() {
    let newName = []
    fs.readdir('./public/',  (err, oldName) => { //读取file文件夹下的文件的名字，oldName是一个数组
        for (let i = 0; i < oldName.length; i++) {
        setTimeout(async function(){
            var image = await fs.readFileSync(`./public/${oldName[i]}`);
            var base64Img = new Buffer.from(image).toString('base64');
                client.generalBasic(base64Img).then(function(result) {
                    let newN = ""
                    let newName = result["words_result"][0].words

                    // console.log("old---"+newName)

                    newN = newName.trim() + "²" 
                    newN=newN.replace("t","7")
                    newN=newN.replace("r","+7") 
                    newN=newN.replace("Q","0") 
                    newN=newN.replace("°","0") 
                    newN=newN.replace("o","0") 
                    newN=newN.replace("O","0")
                    newN=newN.replace("a","0")
                    newN=newN.replace("u","0")
                    newN=newN.replace("U","0")
                    newN=newN.replace("b","0")
                    newN=newN.replace("g","0")
                    newN=newN.replace("d","0")
                    newN=newN.replace("n","0")
                    newN=newN.replace("m0","m")
                    newN=newN.replace("m2","m")
                    newN=newN.replace("mn","m")
                    newN=newN.replace("的2","m")
                    newN=newN.replace("的","m")
                    newN=newN.replace("可","m")
                    newN=newN.replace("加","K")
                    newN=newN.replace("如","K")
                    newN=newN.replace("聊","K")
                  
                    newN=newN.replace("技", "状");
                    newN=newN.replace("横"," 横")
                    newN=newN.replace("楠","横")
                    newN=newN.replace("建","缝")
                    newN=newN.replace("瞳","缝")
                    newN=newN.replace("杜","补")
                    newN=newN.replace("满"," 横")
                    newN=newN.replace("蘅"," 横")
                    newN=newN.replace("蔺"," 横")
                    newN=newN.replace("句","向")
                    newN=newN.replace("向向","向")
                    newN=newN.replace("梨","裂")
                    newN=newN.replace("课","裂")
                    newN=newN.replace("蜂","缝 ")
                    newN=newN.replace("维","缝 ")
                    newN=newN.replace("维","缝 ")
                    newN=newN.replace("窪","缝 ")
                    newN=newN.replace("重", " ");
                    newN=newN.replace("无", " "); 
                    
                    newN=newN.replace("纵向裂躂", " 纵向裂缝"); 
                    newN=newN.replace("纵向裂缝", " 纵向裂缝"); 
                    newN=newN.replace("横回要锋", " 横向裂缝"); 
                    newN=newN.replace("黄向裂缝", " 横向裂缝");
                    newN=newN.replace("横裂缝", " 横向裂缝");
                    newN=newN.replace("3裂缝", "3 横向裂缝");
                    newN=newN.replace("6向裂", "6 横向裂缝");
                    newN=newN.replace("横裂", " 横向裂缝");
                    newN=newN.replace("横白裂缝", " 横向裂缝");
                    newN=newN.replace("横向裂 ", " 横向裂缝 ");
                    newN=newN.replace("块状修补", " 块状修补");
                    newN=newN.replace("条状修补", " 条状修补");
                    newN=newN.replace("条修补", " 条状修补");
                    // newN=newN.replace("横向裂缝", " 横向裂缝");
                    if(newN.trim().substr(0, 1) == "K" || newN.trim().substr(0, 1) == "k"){
                        //满足以K开头
                        if(newN.trim().substr(0, 2) == "K0" || newN.trim().substr(0, 2) == "k0"){
                            //满足以K0开头

                        }else{
                            newN=newN.trim().splice(1, "0");
                        }                    
                    }
                    else{
                        if(newN.trim().substr(0, 1) == "0"){
                            newN="K"+newN.trim()
                        }else{
                            newN="K0"+newN.trim()
                        }  
                    }
                    let num1=Reverse(newN)
                    // console.log(num1)
                    let isNumber=num1.indexOf(" ")
                    // console.log(isNumber)
                    if(isNumber==5){
                        let isTrue=newN.lastIndexOf(".")
                    
                        if(isTrue==-1){
                            newN=Reverse(Reverse(newN).splice(4, "."))   
                        }
                    }else if(isNumber==4){
                        newN=Reverse(Reverse(newN).splice(3, "."))   
                    }
                    // console.log(newN)
            
                    let oldPath = `./public/${oldName[i]}` //原本的路径
                    let newPath = `./public/${title+newN}.jpg` //新路径
                    fs.rename(oldPath, newPath,(err) => { //重命名
                        if (err) {
                            console.log(`重命名失败了--oldname---${oldName[i]}`)
                        } 
                    })
                });
        },3000*i)      
        }
    })
}

function Reverse( str ){
    return str.split('').reverse().join('');
    };


rename()